package vn.com.ltv.lo.daos;

import java.util.ArrayList;

import vn.com.ltv.lo.dtos.DishDTO;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DishDAO {
	protected static final String TABLE_NAME = "Dish";
	protected static final String ID = "rowid";
	protected static final String DSH_ID = "dsh_id";
	protected static final String DSH_NM = "dsh_nm";
	protected static final String DSH_RT_AVRG = "dsh_rt_avrg";
	protected static final String RT_NUMB = "rt_numb";
	protected static final String DSH_DESC = "dsh_desc";
	protected static final String DSH_THUMB = "dsh_thumb";
	protected static final String DSH_PRC = "dsh_prc";
	protected static final String CTG_ID = "ctg_id";
	protected static String TABLE_CREATION_STRING = "create table "
			+ TABLE_NAME + " (" + ID + " integer primary key autoincrement, "
			+ DSH_ID + " text, " + DSH_NM + " text, " + DSH_RT_AVRG
			+ " integer, " + RT_NUMB + " integer, " + DSH_DESC + " text, "
			+ DSH_THUMB + " text, " + DSH_PRC + " integer);";
	protected static String TABLE_DROP_STRING = "drop table if exists"
			+ TABLE_NAME + ";";
	private Context context;

	public DishDAO(Context context) {
		this.context = context;
	}

	public ArrayList<DishDTO> getAll() {
		ArrayList<DishDTO> dishDTOs = new ArrayList<DishDTO>();
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		Cursor cursor = sQLiteDatabase.query(TABLE_NAME, null, null, null,
				null, null, null);
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				DishDTO dishDTO = this.cursorToDishDTO(cursor);
				dishDTOs.add(dishDTO);
				cursor.moveToNext();
			}
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		return dishDTOs;
	}

	public DishDTO get(int id) {
		DishDTO dishDTO = null;
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		Cursor cursor = sQLiteDatabase.query(TABLE_NAME, null, ID + "=?",
				new String[] { String.valueOf(id) }, null, null, null);
		if (cursor.moveToFirst()) {
			dishDTO = this.cursorToDishDTO(cursor);
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		return dishDTO;
	}
	
	public DishDTO get(String idDish) {
		DishDTO dishDTO = null;
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		Cursor cursor = sQLiteDatabase.query(TABLE_NAME, null, DSH_ID + "=?",
				new String[] { String.valueOf(idDish) }, null, null, null);
		if (cursor.moveToFirst()) {
			dishDTO = this.cursorToDishDTO(cursor);
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		return dishDTO;
	}
	
	public ArrayList<DishDTO> getAllByCategoryId(int id) {
		ArrayList<DishDTO> foodDTOs = new ArrayList<DishDTO>();
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		Cursor cursor = sQLiteDatabase.query(TABLE_NAME, null, CTG_ID + "=?", new String[] { String.valueOf(id) },
				null, null, null);
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				DishDTO setDTO = this.cursorToDishDTO(cursor);
				foodDTOs.add(setDTO);
				cursor.moveToNext();
			}
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		return foodDTOs;
	}
	
	
	public ArrayList<DishDTO> getAllByCategoryByName(String name) {
		ArrayList<DishDTO> foodDTOs = new ArrayList<DishDTO>();
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		String query = "SELECT * FROM "+TABLE_NAME+"  WHERE "+DSH_NM+" LIKE '%"+name+"%'";
		Cursor cursor = sQLiteDatabase.rawQuery(query, null);
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				DishDTO setDTO = this.cursorToDishDTO(cursor);
				foodDTOs.add(setDTO);
				cursor.moveToNext();
			}
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		return foodDTOs;
	}

	public void insert(DishDTO dishDTO) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getWritableDatabase();
		ContentValues contentValues = this.dishDTOToContentValues(dishDTO);
		sQLiteDatabase.insert(TABLE_NAME, null, contentValues);
		sQLiteDatabase.close();
		databaseHelper.close();
	}

	/** * @return the number of rows affected */
	public int update(DishDTO dishDTO) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getWritableDatabase();
		int id = dishDTO.getId();
		ContentValues contentValues = this.dishDTOToContentValues(dishDTO);
		int rowCount = sQLiteDatabase.update(TABLE_NAME, contentValues, ID
				+ "=?", new String[] { String.valueOf(id) });
		sQLiteDatabase.close();
		databaseHelper.close();
		return rowCount;
	}

	public void delete(int id) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getWritableDatabase();
		sQLiteDatabase.delete(TABLE_NAME, ID + "=?",
				new String[] { String.valueOf(id) });
		sQLiteDatabase.close();
		databaseHelper.close();
	}

	private DishDTO cursorToDishDTO(Cursor cursor) {
		DishDTO dishDTO = new DishDTO();
//		int id = cursor.getInt(cursor.getColumnIndex(ID));
//		dishDTO.setId(id);
		String dshId = cursor.getString(cursor.getColumnIndex(DSH_ID));
		dishDTO.setDshId(dshId);
		String dshNm = cursor.getString(cursor.getColumnIndex(DSH_NM));
		dishDTO.setDshNm(dshNm);
		float dshRtAvrg = cursor.getFloat(cursor.getColumnIndex(DSH_RT_AVRG));
		dishDTO.setDshRtAvrg(dshRtAvrg);
		int rtNumb = cursor.getInt(cursor.getColumnIndex(RT_NUMB));
		dishDTO.setRtNumb(rtNumb);
		String dshDesc = cursor.getString(cursor.getColumnIndex(DSH_DESC));
		dishDTO.setDshDesc(dshDesc);
		String dshThumb = cursor.getString(cursor.getColumnIndex(DSH_THUMB));
		dishDTO.setDshThumb(dshThumb);
		int dshPrc = cursor.getInt(cursor.getColumnIndex(DSH_PRC));
		dishDTO.setDshPrc(dshPrc);
		String ctg_id  = cursor.getString(cursor.getColumnIndex(CTG_ID));
		dishDTO.setCtg_id(ctg_id);
		return dishDTO;
	}

	private ContentValues dishDTOToContentValues(DishDTO dishDTO) {
		ContentValues contentValues = new ContentValues();
		int id = dishDTO.getId();
		if (id >= 0) {
			contentValues.put(ID, id);
		}
		String dshId = dishDTO.getDshId();
		if (dshId != null) {
			contentValues.put(DSH_ID, dshId);
		}
		String dshNm = dishDTO.getDshNm();
		if (dshNm != null) {
			contentValues.put(DSH_NM, dshNm);
		}
		float dshRtAvrg = dishDTO.getDshRtAvrg();
		if (dshRtAvrg >= 0) {
			contentValues.put(DSH_RT_AVRG, dshRtAvrg);
		}
		int rtNumb = dishDTO.getRtNumb();
		if (rtNumb >= 0) {
			contentValues.put(RT_NUMB, rtNumb);
		}
		String dshDesc = dishDTO.getDshDesc();
		if (dshDesc != null) {
			contentValues.put(DSH_DESC, dshDesc);
		}
		String dshThumb = dishDTO.getDshThumb();
		if (dshThumb != null) {
			contentValues.put(DSH_THUMB, dshThumb);
		}
		int dshPrc = dishDTO.getDshPrc();
		if (dshPrc >= 0) {
			contentValues.put(DSH_PRC, dshPrc);
		}
		String ctg_id = dishDTO.getCtg_id();
		if (ctg_id != null) {
			contentValues.put(CTG_ID, ctg_id);
		}
		return contentValues;
	}
}